package com.changgou.order.service.impl;

import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;


    //购物车添加商品
    @Override
    public void add(String skuId, Integer num, String username) {

        //查询redis中有无商品
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps("cart_" + username).get(skuId);
        if (orderItem != null) {
            orderItem.setNum(orderItem.getNum() + num);

            if (orderItem.getNum() <= 0) {
                redisTemplate.boundHashOps("cart_" + username).delete(skuId);
                return;
            }

            orderItem.setMoney(orderItem.getNum() * orderItem.getPrice());
            orderItem.setPayMoney(orderItem.getNum() * orderItem.getPrice());
        } else {//没有先封装OrderItem
            Sku sku = skuFeign.findById(skuId).getData();
            Spu spu = spuFeign.findById(sku.getSpuId()).getData();
            orderItem = this.sku2OrderItem(sku, spu, num);
        }
        //不管redis有没有,都需要重新写入新数据
        redisTemplate.boundHashOps("cart_" + username).put(skuId, orderItem);
    }

    //查询购物车列表数据
    @Override
    public Map list(String username) {

        Map map = new HashMap<>();
        List<OrderItem> orderItemList = redisTemplate.boundHashOps("cart_" + username).values();
        map.put("orderItemList", orderItemList);

        Integer totalNum = 0;
        Integer totalMoney = 0;
        Integer payMoney = 0;

        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();
            totalMoney += orderItem.getMoney();
            payMoney += orderItem.getPayMoney();
        }

        map.put("totalNum", totalNum);
        map.put("totalMoney", totalMoney);
        map.put("payMoney", payMoney);

        return map;
    }

    @Override
    public void delete(String skuId, String username) {
        redisTemplate.boundHashOps("cart_" + username).delete(skuId);
    }

    //封装数据到orderItem中
    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num * orderItem.getPrice());       //单价*数量
        orderItem.setPayMoney(num * orderItem.getPrice());    //实付金额
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight() * num);           //重量=单个重量*数量
        //分类ID设置
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }
}
